<?php

//defined('BASEPATH') OR exit('No direct script access allowed');

class Shop extends MY_Test
{
    //    public function __construct() {
    //        parent::__construct();
    //    }

    //    protected 或 _开头的 不能被请求

    public function index() {
        //        echo 'i am shop controller';
        $data['item'] = ['title' => 'data3333' ,'body' => 'helloworld'];
        $this->load->view('shop/shop' ,$data);
    }

    public function test1($name ,$id) {
        echo 'test';
        echo $name;
        echo $id;
    }

    public function test2() {
        $this->load->model('crm_custom');
        $res = $this->crm_custom->get_last_ten_entries();
        dump($res);
    }

    public function test3() {
        //        $string = $this->load->view('shop/shop', '', TRUE);//只返回不显示
        //        file_put_contents('aaa',$string);
        $this->load->vars('aaa' ,'我是aaa');
        $this->load->view('shop/shop');
    }

    public function test4($id) {
        echo $id;
        dump($this->uri->segment(3));//用于获取某一段的值
        dump($this->input);//用于获取某一段的值

        dump($this->input->post());
        dump($this->input->server('DOCUMENT_ROOT'));
        $this->load->helper('array');
        dump($this->load);
    }

    public function test5() {
        //1.直接
        //装载数据库操作类到超级对象,默认属性是db
        $this->load->database();
        //        dump($this->db);
        $sql = "SELECT * FROM `advisory` LIMIT 10";
        $res = $this->db->query($sql);//返回对象
        //        dump($res->result());//是一个个对象
        $data['user_list'] = $res->result();//返回对象
        $data['user_list'] = $res->result_array();//返回数组
        $data['user_list'] = $res->row();//返回第一个对象
        $this->load->view('test5' ,$data);

        echo ENVIRONMENT;


    }

    public function test6() {
        $sql = "INSERT INTO `b2b`.`advisory` (`id`, `name`, `mobile_phone`, `company_name`, `linkMen`, `remark`, `created`) VALUES (NULL, 'heven', '15291181898', '铱云科技', NULL, NULL, '2017-10-12 16:37:19')";

        $bool = $this->db->query($sql);
        if ($bool) {
            echo $this->db->affected_rows();
            echo $this->db->insert_id();
        } else {
            echo 'no';
        }
    }

    public function test7() {
        //        $name = $this->input->post('name');
        //        $pass = 'abbcc';
        $data = [  //只支持一个数组
            'lili' ,
            '123'
        ];
        $sql = "INSERT INTO advisory (name, mobile_phone) VALUE (?,?)";
        $bool = $this->db->query($sql ,$data);
        if ($bool) {
            echo $this->db->affected_rows();
            echo $this->db->insert_id();
        } else {
            echo 'no';
        }
    }


    public function test8() {
        //2.AR模型 model
        //1.查询
        //        $res = $this->db->get('advisory');//返回结果集对象
        //        dump($res);
        //        foreach ($res->result() as $r) {
        //            echo $r->id;
        //        }

        //2.insert
        $data = [
            'name' => 'adfddafa' ,
            'mobile_phone' => 33333
        ];
        $bool = $this->db->insert('advisory' ,$data);
        if ($bool) {
            echo $this->db->affected_rows();
            echo $this->db->insert_id();
        } else {
            echo 'no';
        }

        //3.update
        $data = [
            'name' => 'adfddafa' ,
            'mobile_phone' => 8888
        ];
        $bool = $this->db->update('advisory' ,$data ,['id' => $this->db->insert_id()]);
        dump($bool);

        //4.delete
        $this->db->delete('advisory' ,['id' => 1054]);

        //5.连贯操作
        $res = $this->db->select('id,name')
            ->from('advisory')
            ->where('id >=' ,1054)//一定要空格,正则匹配的
            ->order_by('id desc')
            ->limit(3 ,2)//这个和mysql是反的,跳过2条取3条
            ->get();
        //        echo $this->db->get_compiled_select();//SELECT `id`, `name` FROM `advisory` WHERE `id` >= 1054 ORDER BY `id` desc LIMIT 3, 2
        dump($res->result());

        //显示最近一条
        echo $this->db->last_query();

        //条件
        //        $res = $this->db->where('name','adfddafa')->get('advisory');
        $res = $this->db->where([
            'name' => 'adfddafa' ,
            'id >' => 1063
        ])->get('advisory');

        dump($res->result());
    }

    public function test9(){
        $this->load->model('advisory');
        dump($this->advisory->getAll());
    }

    public function test10(){
        echo 'back';
        $this->load->library('aes');
        $this->aes->test();
    }
}